<template>
  <div class="goodslist-container">
    <div class="goods-item" v-for="item in goodslist" :key="item.id" @click="getDetail(item.id)">
      <img :src="item.img_url" alt="">
      <h3 class="title">{{item.title}}</h3>
      <div class="info">
        <p class="price">
          <span class="newPrice">¥{{item.market_price}}</span>
          <span class="oldPrice">¥{{item.sell_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{item.stock_quantity}}件</span>
        </p>
      </div>
    </div>    

    <mt-button type="danger" size="large" plain @click="getmore" class="button">加载更多</mt-button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      pageIndex:1,
      goodslist:[]
    }
  },
  methods:{
    getAllGoods(){
      this.$http.get('api/getgoods?pageindex='+this.pageIndex).then(result=>{
        if(result.body.status === 0){
          // console.log(result.body.message)
          this.goodslist = this.goodslist.concat(result.body.message)
        }
      })
    },
    getmore(){
      this.pageIndex++;
      this.getAllGoods();
    },
    getDetail(id){
      this.$router.push({name:'goodsinfo',params:{id}})
    }
  },
  created(){
    this.getAllGoods()
  }
}
</script>
<style lang="scss" scoped>
  .goodslist-container {
    display: flex;
    flex-wrap: wrap;
    padding:7px;
    justify-content: space-between;
    .goods-item {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      width: 49%;
      box-shadow: 0 0 8px #ccc;
      padding: 2px;
      margin: 5px 0;
      img {
        width: 100%;
      }
      h3{
        font-size: 14px;
        line-height: 18px;
        text-align: left;
      }
      .info {
        background-color: #eee;
        p{
          padding: 5px;
          margin: 0;
        }
        .price {
          .newPrice {
            font-size: 16px;
            font-weight: 700;
            color: red;
            margin-right: 10px;
          }
          .oldPrice {
            font-size: 12px;
            text-decoration: line-through;
          }
        }
        .sell {
          display: flex;
          justify-content: space-between;
        }
      } 
      }
      .button {
        margin: 10px 0;
      }
    }
</style>